<?php

namespace Home\Model\Ajax;

class SendMobileCode extends AbstractAjax
{
    // 发送手机验证码，需验证手机是否存在
    public function handle()
    {
        $mobile_post = I('request.mobile', '', 'trim');
        $in_user = I('request.in_user', '1', 'intval');//默认给已经注册过的手机发送验证码
        if (!preg_match("/^1[123456789]{1}\d{9}$/", $mobile_post)) {
            $ajax['status'] = 0;
            $ajax['content'] = "手机号{" . $mobile_post . "}格式错误！";
            return $ajax;
        } else {
            if ($in_user == '1') {
                $info = D('User')->get_user_one($mobile_post, false, 2);
                if (empty($info)) {
                    $ajax['status'] = 0;
                    $ajax['content'] = "手机号[" . $mobile_post . "]未注册！";
                    return $ajax;
                }
            }
            $mobile_code = strval(rand(1000, 9999));
            $params = array('rand' => $mobile_code);
            $send = send_sms($mobile_post, $params, 'mobile_code');
            if ($send['status'] == '0') {
                $ajax['status'] = 0;
                $ajax['content'] = $send['content'];
                return $ajax;
            }
            session('mobile_code', $mobile_code);
            session('mobile_post', $mobile_post);
            session('code_err_is3', null);
            $ajax['status'] = 1;
            $ajax['content'] = "验证码发送成功！";
            return $ajax;
        }
    }
}